package sk.tuke.constraintbuilder.editor;

import java.io.File;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.ide.FileStoreEditorInput;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.ui.part.MultiPageEditorPart;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;

import sk.tuke.constraintbuilder.model.Constraint;
import sk.tuke.constraintbuilder.model.Model;
import sk.tuke.constraintbuilder.model.Ruleset;
import sk.tuke.constraintbuilder.model.Template;
import sk.tuke.constraintbuilder.utils.MyUtils;

public class MultiPageEditor extends MultiPageEditorPart {
	
	public static final String ID = "sk.tuke.constraintbuilder.editor.multipage";
	
	private EditorPart editor;
	private TextEditor textEditor;
	private EditorInput editorInput;
	private FileStoreEditorInput fsEditorInput;

	public MultiPageEditor() {
		super();
	}
	
	void createGUIPage() {
		try {
			if(editorInput.getInputElement() instanceof Constraint){
				editor = new ConstraintEditor(this);
				int index = addPage(editor, editorInput);
				setPageText(index, editor.getTitle());
			}
			else if(editorInput.getInputElement() instanceof Template){
				editor = new TemplateEditor(this);
				int index = addPage(editor, editorInput);
				setPageText(index, editor.getTitle());
			}
			else if(editorInput.getInputElement() instanceof Ruleset){
				editor = new RulesetEditor(this);
				int index = addPage(editor, editorInput);
				setPageText(index, editor.getTitle());
			}
		} catch (PartInitException e) {
			MessageDialog.openError(getSite().getShell(), "Error creating nested constraint builder", "Error creating nested constraint builder");
		}
	}
	
	void createTextEditorPage() {
		if(fsEditorInput!=null){
			try {
				textEditor = new TextEditor();
				int index = addPage(textEditor, fsEditorInput);
				setPageText(index, textEditor.getTitle());
			} catch (PartInitException e) {
				ErrorDialog.openError(
					getSite().getShell(),
					"Error creating nested text editor",
					null,
					e.getStatus());
			}
		}
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub
	}

	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		if (!(input instanceof EditorInput)) {
			throw new RuntimeException("Wrong input");
		}
		this.editorInput = (EditorInput) input;
		super.init(site, editorInput);
		fileStoreEditorInputInit();
	}
	
	private void fileStoreEditorInputInit(){
		File file = Model.getModel().getFile(this.editorInput.getInputElement().getId());
		if(file!=null){
			IPath path = Path.fromOSString(file.getAbsolutePath());			
			IFileStore fs = EFS.getLocalFileSystem().getStore(path);
			fsEditorInput = new FileStoreEditorInput(fs);
		}
	}

	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	@Override
	public void setFocus() {
	}

	@Override
	protected void createPages() {
        createGUIPage();
        createTextEditorPage();
	}
	
	protected void pageChange(int newPageIndex) {
        super.pageChange(newPageIndex);
        if (newPageIndex == 1) {
        	textEditor.setInput(fsEditorInput);
        	textEditor.getAction(ITextEditorActionConstants.REFRESH).run();
        }
	}
	
	public void gotoMarker(IMarker marker) {
        setActivePage(0);
        IDE.gotoMarker(getEditor(0), marker);
	}

	public void refresh(EditorInput editorInput) {
		if(getPageCount()==1){
			this.editorInput=editorInput;
			fileStoreEditorInputInit();
			createTextEditorPage();
		}
	}
	
	public void setTitleName(String name){
		setPartName(name);
	}

}
